package edu.cicese.tm;

import com.sun.awt.AWTUtilities;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 19/03/11
 * Time: 10:10 PM
 */
public class DialogAnnotation extends JDialog {
	private FrameTagCreation frmTagCreation;

	private JTextArea taAnnotation;

	public DialogAnnotation(FrameTagCreation owner) {
		super(owner);
		frmTagCreation = owner;
		AWTUtilities.setWindowOpacity(frmTagCreation, 0.75f);

		setTitle("Anotación");
		setSize(300, 167);
		setIconImage(new ImageIcon(getClass().getClassLoader().getResource("icons/icon.png")).getImage());
		setResizable(false);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeWindow(false);
			}
		});

		taAnnotation = new JTextArea();
		taAnnotation.setFont(new Font("Sans-Serif", Font.PLAIN, 13));
		taAnnotation.setLineWrap(true);
		taAnnotation.setForeground(Color.darkGray);
		taAnnotation.setText(frmTagCreation.annotation);
		taAnnotation.setCaretPosition(0);

		JScrollPane spAnnotation = new JScrollPane(taAnnotation, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		spAnnotation.setBounds(50, 5, 240, 100);

		JPanel pAnnotation = new JPanel();
		pAnnotation.setLayout(null);
		pAnnotation.setSize(295, 150);
		pAnnotation.add(spAnnotation);

		JLabel lblAnnotation = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("icons/annotation.png")));
		SwingUtil.addLabel(lblAnnotation, 5, 35, 40, 50, "", pAnnotation, null);

		JButton btnAccept = new JButton();
		addButton(btnAccept, 88, 105, 90, 32, "Aceptar", "accept", pAnnotation, null);
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionAccept();
			}
		});

		JButton btnCancel = new JButton();
		addButton(btnCancel, 153, 105, 90, 32, "Cancelar", "cancel", pAnnotation, null);
	    btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});

		Rectangle window = getBounds();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
		getContentPane().add(pAnnotation);
		setVisible(true);
	}

	private void closeWindow(boolean acceptOrCancel) {
		if (acceptOrCancel) {
			setVisible(false);
			dispose();
		}
		frmTagCreation.setEnabled(true);
		frmTagCreation.toFront();
		AWTUtilities.setWindowOpacity(frmTagCreation, 1f);
	}

	private void actionAccept() {
		frmTagCreation.annotation = taAnnotation.getText().trim();
		if (!frmTagCreation.annotation.equals("")) {
			frmTagCreation.chbAnnotation.setSelected(true);
		}
		closeWindow(true);
	}

	private void actionCancel() {
		closeWindow(true);
	}

	//! Adds a JButton
	private void addButton(JButton button, int x, int y, int width, int height,
	                              String toolTipText, String iconName, JPanel pane, Container container) {
		Icon icon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + ".png"));
		Icon rIcon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + "O.png"));
		Icon pIcon = new ImageIcon(getClass().getClassLoader().getResource("icons/" + iconName + "P.png"));
		SwingUtil.addButton(button, x, y, width, height, toolTipText, icon, rIcon, pIcon, pane, container);
	}
}
